const fs = require('fs')
const {resolve} = require('path')

// 复制文件夹
function copyFolder(input, output) {
    if (isFillExist(output)) {
        rmFill(output)
    }
    const stat = fs.statSync(input)
    if (stat.isDirectory()) {
        fs.mkdirSync(output)
        const readdir = fs.readdirSync(input, {encoding:'utf8'})
        readdir.forEach(item => {
            copyFolder(resolve(input, `./${item}`), resolve(output, `./${item}`))
        })
    } else if (stat.isFile()) {
        copyFill(input, output)
    }
}

// copy文件
function copyFill(inputNmae, outputNmae) {
    fs.copyFileSync(inputNmae, outputNmae)
}

// 删除文件夹
function rmFill(fillName) {
    fs.rmSync(fillName, {
        force: true,
        recursive: true
    })
}

// 判断文件是否存在
function isFillExist(fillName) {
    try {
        if (fs.statSync(fillName)) {
            return true
        }
    } catch (e) {
        return false
    }
}
module.exports =copyFolder
// copyFolder(resolve(__dirname, '../src/PublicFolder'), resolve(__dirname, '../app/PublicFolder'))
